package com.itqf.Service.User.Impl;

import com.itqf.Dao.UserMapper;
import com.itqf.Service.User.UserService;

import com.itqf.Util.JwtUtile.JwtUtils;
import com.itqf.Util.MailUtils;
import com.itqf.Util.Md5Util;
import com.itqf.Util.RandomStringTLUtils;
import com.itqf.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService
{
    @Resource
    private UserMapper userMapper;


    /**
     * 通过查找我的主页
     * */
    @Override
    public User FindFindByuid(String uid) {
        return userMapper.FindFindByuid(uid);
    }

    @Override
    public User findbytoken(String token) {
        return userMapper.findbytoken(token);
    }
    /*
    开通会员   李朝银
     */
    @Override
    public int updetfindbyuserstate(String userid) {
        return userMapper.updetfindbyuserstate(userid);
    }


    @Override
    public void updateusergread(String userid, int gradeid) {
        Map map=new HashMap();
        map.put("userid",userid);
        map.put("gradeid",gradeid);
        userMapper.updateusergread(map);
    }

    @Override
    public User findusers(String userid) {
        return userMapper.findusers(userid);
    }

    @Override
    public String finduserandgrade(String userid) {
        return userMapper.finduserandgrade(userid);
    }


    /*
   jmhh========
    */
    @Override
    public User FindFindByuid(int uid) {
        return null;
    }
    /**
     * 邮箱找回密码
     * @param userName
     * @return
     */
    @Override
    public String getUserByUserName(String userName) {
        User user = userMapper.getUserByUserName(userName);
        if (user==null) {
            return "发送失败,邮箱有误";
        }
        String newPassWord = RandomStringTLUtils.randomNumeric(6);
        MailUtils.sendMail(userName,"你的密码是:"+newPassWord,"找回密码");
        try {
            String passWordByMD5 = Md5Util.encodeByMd5(newPassWord);
            User user1 = new User();
            user1.setEmail(userName);
            user1.setUserpassword(passWordByMD5);
            userMapper.updatePassWordByEmail(user1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "密码已发送邮件";
        //return userMapper.getUserByUserName(userName);
    }

    /**
     * 邮箱账号密码登录
     * @param userName
     * @param passWord
     * @return
     */
    @Override
    public User getUserByUser(String userName, String passWord) {

        User user = userMapper.getUserByUser(userName);
        return user;
        /*if (user==null) {
            return user;
            *//*return"{\n" +
                    "   \"loginMessages\":\n" +
                    "    {\n" +
                    "       \"correctMessages\":\"\" ,\n" +
                    "       \"erroeMessages\":\n" +
                    "         {\"emailMessages\":\"邮箱尚未注册\",\"emailPass\":\"\"}\n" +
                    "    },\n" +
                    "    \"userToken\":\"\"\n" +
                    " }";*//*
        }
        try {
            passWord = Md5Util.encodeByMd5(passWord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!passWord.equals(user.getUserpassword())) {
            return "{\n" +
                    "   \"loginMessages\":\n" +
                    "    {\n" +
                    "       \"correctMessages\":\"\" ,\n" +
                    "       \"erroeMessages\":\n" +
                    "         {\"emailMessages\":\"\",\"emailPass\":\"邮箱密码不正确\"}\n" +
                    "    },\n" +
                    "    \"userToken\":\"\"\n" +
                    " }";
        }

        return "{\n" +
                "   \"loginMessages\":\n" +
                "    {\n" +
                "       \"correctMessages\":\"登录成功\" ,\n" +
                "       \"erroeMessages\":\n" +
                "         {\"emailMessages\":\"\",\"emailPass\":\"\"}\n" +
                "    },\n" +
                "    \"userToken\":\""+user.getUserToken()+"\"\n" +
                " }";*/
    }

    /**
     * 邮箱账号注册
     * @param user
     * @return
     */
    @Override
    @Transactional
    public String insert(User user) {
        User getUser = userMapper.getUserByUser(user.getUsername());
        if (getUser!=null) {
            return "邮箱已被注册";
        }

        int i = userMapper.insertUser(user);
        //user.setGetHeadImg("a.jpg");
        String token = JwtUtils.geneJsonWebTokenByEmail(user);
        user.setUserToken(token);
        userMapper.updateUserByToken(user);
        System.out.println(user);

        return "注册成功";
    }

    /**
     * 邮箱修改密码
     * @param user
     * @return
     */
    @Override
    @Transactional
    public String updatePassWordByEmail(User user) {
        userMapper.updatePassWordByEmail(user);
        return "修改成功";
    }

    @Override
    @Transactional
    public int phoneUpdatePassword(User user) {
        return userMapper.phoneUpdatePassword(user);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }

    @Override
    @Transactional
    public int updateUserByToken(User user) {
        return userMapper.updateUserByToken(user);
    }

    @Override
    @Transactional
    public int insertUserByPhone(User user) {
        return userMapper.insertUserByPhone(user);
    }

    @Override
    @Transactional
    public int addUserByPhoneAndPassWord(User user) {
        return userMapper.addUserByPhoneAndPassWord(user);
    }

    /*
    jmhh====================
     */

}
